<?php
/**
 * This file is part of the UXLabs project.
 *
 * (c) UXLabs project <http://code.uxlabs.me>
 *
 */

namespace uxl\app\api\models;

use uxl\app\api\components\Parameters;
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\db\Query;

/**
 * app api 访问日志
 *
 * @property int $id AUTO_INCREMENT
 * @property string $accessType 访问类型
 * @property string $accessKey 访问密钥
 * @property string $userAuthKey 用户登录授权密钥
 * @property int $reqTimestamp 请求提交的时间戳
 * @property string $reqOnce 请求提交的随机字符串
 * @property string $reqApi 请求的接口
 * @property string $reqParams 请求的参数值
 * @property string $reqTime 请求时间
 * @property int $respStatus 返回结果状态
 * @property string $respContent 返回结果内容
 * @property string $respTime 返回结果时间
 *
 * @package uxl\app\api\models
 * @author ice deng <ice.deng@qq.com>
 */
class AccessLog extends ActiveRecord
{
    /** @inheritdoc */
    public static function tableName()
    {
        return '{{%app_api_access_log}}';
    }

    /**
     * 记录请求
     * @param string $accessType
     * @param Parameters $parameters
     * @param string $apiName
     * @return AccessLog|bool
     */
    public function request($accessType, $parameters, $apiName)
    {
        $row = new static();
        $row->accessType = $accessType;
        $row->accessKey = $parameters->accessKey;
        $row->userAuthKey = $parameters->userAuthKey;
        $row->reqTimestamp = $parameters->timestamp;
        $row->reqOnce = $parameters->once;
        $row->reqApi = $apiName;
        $row->reqParams = $parameters->toJson();
        if ($row->save(false)) {
            return $row;
        }
        return false;
    }

    /**
     * 记录返回结果
     * @param $id
     * @param $status
     * @param $content
     */
    public function response($id, $status, $content)
    {
        static::updateAll(
            [
                'respStatus' => $status,
                'respContent' => $content,
                'respTime' => new Expression('NOW()'),
            ],
            'id = :id',
            ['id' => $id]
        );
    }

    /**
     * 是否有重复记录
     * @param $type
     * @param $accessKey
     * @param $timestamp
     * @param $once
     * @return array|bool
     */
    public function repeatRequest($type, $accessKey, $timestamp, $once)
    {
        $q = new Query();
        return $q->from(static::tableName())
            ->select('id')
            ->where([
                'accessType' => $type,
                'accessKey' => $accessKey,
                'reqTimestamp' => $timestamp,
                'reqOnce' => $once
            ])->one();
    }
}

/**
CREATE TABLE IF NOT EXISTS `uxlabs_tpl_base`.`app_api_access_log` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`accessType` VARCHAR(255) NOT NULL COMMENT '访问类型',
`accessKey` CHAR(32) NOT NULL COMMENT '访问密钥',
`userAuthKey` VARCHAR(255) NULL COMMENT '用户登录授权密钥',
`reqTimestamp` BIGINT NOT NULL COMMENT '请求提交的时间戳',
`reqOnce` VARCHAR(255) NOT NULL COMMENT '请求提交的随机字符串',
`reqApi` VARCHAR(255) NOT NULL COMMENT '请求的接口',
`reqParams` LONGTEXT NULL COMMENT '请求的参数值',
`reqTime` TIMESTAMP NOT NULL DEFAULT NOW() COMMENT '请求时间',
`respStatus` INT NULL COMMENT '返回结果状态',
`respContent` LONGTEXT NULL COMMENT '返回结果内容',
`respTime` DATETIME NULL COMMENT '返回结果时间',
PRIMARY KEY (`id`),
INDEX `ix_ app_api_access_log_once` (`accessType` ASC, `accessKey` ASC, `reqTimestamp` ASC, `reqOnce` ASC))
ENGINE = InnoDB
COMMENT = 'app api 访问日志'
 */